<?php

namespace App\Http\Controllers;

//use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Chuban;
use App\Models\Chubaninfo;
use App\Models\Book;
use App\Models\Chubanzengshu;
use App\Models\Chubanzengshuliuyan;
use App\Models\UsersReg;

use Request;
use Session;
use Auth;
use Redirect;

class hangyeController extends Controller
{
    public function index()
    {
        // 显示行业合作首页

        $uid = Session::get('nameid');
        $chuban = Chuban::where('uid',$uid)->get();
        if(!empty($chuban)){
            //判断是否已经添加认证资料，没添加就去添加，添加了跳转到认证管理页面
            $rz = 1;
        }

        return view('hangye.index',array(
            'rz' => $rz
        ));
    }

    public function chubaninfo($chuban_id)
    {
        // 显示出版社内容介绍页面
        $chubaninfos = Chubaninfo::where('chuban_id',$chuban_id)->get();
        if(empty($chubaninfos[0])){
            abort(404);
        }

        return view('hangye.chubaninfo',array(
            'chubaninfos' => $chubaninfos
        ));
    }

    public function renzhengchuban()
    {
        // 添加出版社信息
        $uid = Session::get('nameid');        
        $chuban = Chuban::where('uid',$uid)->get();
        if(!empty($chuban[0])){
            //如果会员已经添加过认证信息
            return "您已经添加过认证信息了，不可以重复添加！";
        }

        return view('hangye.renzheng_chuban');
    }

    public function saverenzhengchuban()
    {
        // 保存添加出版社信息
        //$name = Session::get('name');
        $uid = Session::get('nameid');

        $user = Chuban::where('uid', $uid)->exists();
        if($user){
            //本会员已经提交过了认证资料，不能重复提交。
            return '你的认证资料已经提交了，不能重复提交！';
        }

        //保存认证资料
        $chubans = new Chuban;

        $chubans->uid = $uid;
        $chubans->danwei_xingzhi = request::input('danwei_xingzhi');
        $chubans->zhengjian_leixing = request::input('zhengjian_leixing');
        $chubans->danwei_num = request::input('danwei_num');
        $chubans->danwei_name = request::input('danwei_name');
        $chubans->danwei_dizhi = request::input('danwei_dizhi');
        $chubans->fuzeren = request::input('fuzeren');
        $chubans->fuzeren_zhengjian = request::input('fuzeren_zhengjian');
        $chubans->fuzeren_num = request::input('fuzeren_num');
        $chubans->dianhua = request::input('dianhua');
        $chubans->qq = request::input('qq');
        $chubans->email = request::input('email');
        $chubans->beizhu = request::input('beizhu');

        
        //$dirName = 'chuban\\'; //windows 系统下目录结构
        $dirName = "chuban/renzheng/" . $uid; // linux 系统下的目录结构

        //上传身份证正面id1
        //取得上传文件
        $id1 = request::file('id1');        
        //得到上传文件的扩展名
        $id1_entension = $id1 -> getClientOriginalExtension();        
        $id1_filename = "uid" . $uid . "-id1"; //文件名字
        $id1_newName = $id1_filename . '.' . $id1_entension;
        $id1_path = $id1 -> move($dirName, $id1_newName);

        $chubans->id1 = $id1_path;

        //上传身份证反面id2
        //取得上传文件
        $id2 = request::file('id2');        
        //得到上传文件的扩展名
        $id2_entension = $id2 -> getClientOriginalExtension();        
        $id2_filename = "uid" . $uid . "-id2"; //文件名字
        $id2_newName = $id2_filename . '.' . $id2_entension;
        $id2_path = $id2 -> move($dirName, $id2_newName);

        $chubans->id2 = $id2_path;

        //上传工商营业执照
        //取得上传文件
        $gs = request::file('gs');        
        //得到上传文件的扩展名
        $gs_entension = $gs -> getClientOriginalExtension();        
        $gs_filename = "uid" . $uid . "-gs"; //文件名字
        $gs_newName = $gs_filename . '.' . $gs_entension;
        $gs_path = $gs -> move($dirName, $gs_newName);

        $chubans->gs = $gs_path;

        $chubans->mark = 0;  //0为未通过审核;1通过审核
        $chubans->add_time = time(); //获取当前时间戳
        $chubans->save();

        return redirect('hangye/rzcb');
    }

    public function rzcb()
    {
        //出版认证资料提交后显示的页面
        return "资料提交成功，请等待管理员审核！";
    }

    public function chubanlist()
    {
        //出版社审核列表
        $pre_page = 15;
        $chubanlists = Chuban::orderBy('id', 'desc')->paginate($pre_page);

        return view('admin.chubanlist', array(
            'chubanlists' => $chubanlists
            ));
    }

    public function chubanlist_sh($id)
    {
        //出版社审核列表-审核
        $chuban = Chuban::find($id);

        $chuban->mark = 1; //通过审核
        $chuban->save();

        return "通过审核！";
    }

    public function chubanlist_ck($id)
    {
        //出版社审核列表-查看认证信息
        $chuban = Chuban::find($id);

        return view('admin.chubanlist_ck',array(
            'chuban' => $chuban
        ));
    }

    public function chubanlist_del($id)
    {
        //出版社审核列表-删除
        $chuban = Chuban::find($id);
        //获得要删除的文件名字
        $picName = $chuban->id1;
        if(file_exists($picName))
            {
                //删除文件-id正面
                unlink($picName);
            }

        $picName = $chuban->id2;
        if(file_exists($picName))
            {
                //删除文件-id反面
                unlink($picName);
            }

        $picName = $chuban->gs;
        if(file_exists($picName))
            {
                //删除文件-工商营业执照图片
                unlink($picName);
            }

        //同时删除目录 - 以uid建立的目录
        $uid = $chuban->uid;
        rmdir('chuban/renzheng'.$uid); 

        $chuban->delete();
        return "删除成功！";
    }

    public function chuban_main()
    {
        //出版社/出版公司管理页面
        $uid = Session::get('nameid');

        $chuban = Chuban::where('uid',$uid)->get();    
        if(empty($chuban[0])){
            //如果会员没有添加认证信息
            //return "没有权限访问！";
            return redirect('hangye/renzheng_chuban'); //跳转到发布认证信息页面
        }

        return view('hangye.chuban_main',array(
            'chuban' => $chuban
        ));
    }

    public function chakan_renzhengchuban()
    {
        //出版社/出版公司认证资料查看浏览

        $uid = Session::get('nameid');        
        $chuban = Chuban::where('uid',$uid)->get();
        if(empty($chuban[0])){
            //如果会员没有添加认证信息
            //return "没有权限访问！";
            return redirect('u/' . $uid); //返回到会员中心首页
        }
        // elseif ($chuban[0]->mark  ==  0) {
        //     //会员没有认证通过
        //     //return "没有权限访问！";
        //     return redirect('u/' . $uid); //返回到会员中心首页
        // }
        return view('hangye.chakan_renzhengchuban',array(
            'chuban' => $chuban
        ));
    }

    public function chakan_chubaninfo()
    {
        $uid = Session::get('nameid');
        $chuban = Chuban::where('uid',$uid)->get();
        if(empty($chuban[0])){
            //如果会员没有添加认证信息
            //return "没有权限访问！";
            return redirect('u/' . $uid); //返回到会员中心首页
        }
        $chuban_mark = $chuban[0]->mark; //认证标识
        if($chuban_mark == 0) {
            //出版没有认证审核
            return view('hangye.chuban_main',array(
                'chuban' => $chuban
            ));
        }
        $chubaninfo = Chubaninfo::where('uid', $uid)->get();
        if(!empty($chubaninfo[0])){
            //如果出版信息添不为空(已经发布了出版公司介绍信息)
            $chubaninfo_mark = 1;
        }else{
            $chubaninfo_mark = 0;
        }
        return view('hangye.chakan_chubaninfo',array(
            'chubaninfo_mark'   =>  $chubaninfo_mark,
            'chubaninfo'        =>  $chubaninfo
        ));
    }

    public function fabu_chubaninfo()
    {
        //出版社/出版公司信息-添加信息发布页面
        $uid = Session::get('nameid');
        $chuban = Chuban::where('uid', $uid)->get();

        if(empty($chuban[0])){
            //如果会员没有添加认证信息
            //return "没有权限访问！";
            return redirect('u/' . $uid); //返回到会员中心首页
        }

        return view('hangye.fabu_chubaninfo');
    }
    
    public function save_chubaninfo()
    {
        //保存出版公司介绍信息
        //$name = Session::get('name');
        $uid = Session::get('nameid');

        $user = Chubaninfo::where('uid', $uid)->exists();
        if($user){
            //本会员已经提交过了认证资料，不能重复提交。
            return '您出版社/公司的信息已经提交了，不能重复提交！';
        }

        $Chubaninfos = new Chubaninfo;
        $Chubaninfos->uid = $uid;
        $Chubaninfos->danwei_name = request::input('danwei_name');
        $Chubaninfos->chengli_time = request::input('chengli_time');
        $Chubaninfos->dianhua = request::input('dianhua');
        $Chubaninfos->province = request::input('province');
        $Chubaninfos->city = request::input('city');
        $Chubaninfos->town = request::input('town');
        $Chubaninfos->dizhi = request::input('dizhi');
        $Chubaninfos->x_lng = request::input('x_lng');
        $Chubaninfos->y_lat = request::input('y_lat');
        $Chubaninfos->jieshao = request::input('jieshao');

        //$dirName = 'chuban\\'; //windows 系统下目录结构
        $dirName = "chuban/gongsi/" . $uid; // linux 系统下的目录结构

        //上传公司logo
        //取得上传文件
        $logo = request::file('logo');        
        //得到上传文件的扩展名
        $logo_entension = $logo -> getClientOriginalExtension();        
        $logo_filename = "uid" . $uid . "-logo"; //文件名字
        $logo_newName = $logo_filename . '.' . $logo_entension;
        $logo_path = $logo -> move($dirName, $logo_newName);

        $Chubaninfos->logo = $logo_path;

        //上传公司环境照片
        //取得上传文件
        $huanjing1 = request::file('huanjing1');        
        //得到上传文件的扩展名
        $huanjing1_entension = $huanjing1 -> getClientOriginalExtension();        
        $huanjing1_filename = "uid" . $uid . "-huanjing1"; //文件名字
        $huanjing1_newName = $huanjing1_filename . '.' . $huanjing1_entension;
        $huanjing1_path = $huanjing1 -> move($dirName, $huanjing1_newName);

        $Chubaninfos->huanjing1 = $huanjing1_path;
        $Chubaninfos->add_time = time();
        $Chubaninfos->save();

        //保存数据后跳转到展示页面
        return redirect('hangye/chubaninfo/'. $Chubaninfos->chuban_id); 
    }

    public function edit_chubaninfo()
    {
        //编辑出版信息
        //$name = Session::get('name');
        $uid = Session::get('nameid');

        $user = Chubaninfo::where('uid', $uid)->exists();
        if(!$user){
            //如果没有发布信息
            return '您没有可以编辑的出版信息！';
        }

        $chubaninfos = Chubaninfo::where('uid',$uid)->get();
        return view('hangye.edit_chubaninfo',array(
            'chubaninfos' => $chubaninfos
        ));
    }

    public function saveedit_chubaninfo()
    {
        //保存编辑的出版信息
        //$name = Session::get('name');
        $uid = Session::get('nameid');

        $Chubaninfos = Chubaninfo::where('uid',$uid)->first();

        $Chubaninfos->danwei_name = request::input('danwei_name');
        $Chubaninfos->chengli_time = request::input('chengli_time');
        $Chubaninfos->dianhua = request::input('dianhua');
        $Chubaninfos->province = request::input('province');
        $Chubaninfos->city = request::input('city');
        $Chubaninfos->town = request::input('town');
        $Chubaninfos->dizhi = request::input('dizhi');
        $Chubaninfos->x_lng = request::input('x_lng');
        $Chubaninfos->y_lat = request::input('y_lat');
        $Chubaninfos->jieshao = request::input('jieshao');

        //$dirName = 'chuban\\'; //windows 系统下目录结构
        $dirName = "chuban/gongsi/" . $uid; // linux 系统下的目录结构

        //判断有没有上传文件
        if(request::hasFile('logo'))
        {

        //上传公司logo
        //取得上传文件
        $logo = request::file('logo');
        //return $logo;        
        //得到上传文件的扩展名
        $logo_entension = $logo -> getClientOriginalExtension();        
        $logo_filename = "uid" . $uid . "-logo"; //文件名字
        $logo_newName = $logo_filename . '.' . $logo_entension;
        $logo_path = $logo -> move($dirName, $logo_newName);

        $Chubaninfos->logo = $logo_path;

        }

        //判断有没有上传文件
        if(request::hasFile('huanjing1'))
        {

        //上传公司环境照片
        //取得上传文件
        $huanjing1 = request::file('huanjing1');        
        //得到上传文件的扩展名
        $huanjing1_entension = $huanjing1 -> getClientOriginalExtension();        
        $huanjing1_filename = "uid" . $uid . "-huanjing1"; //文件名字
        $huanjing1_newName = $huanjing1_filename . '.' . $huanjing1_entension;
        $huanjing1_path = $huanjing1 -> move($dirName, $huanjing1_newName);

        $Chubaninfos->huanjing1 = $huanjing1_path;

        }

        $Chubaninfos->add_time = time();
        $Chubaninfos->save();

        //return '更新信息成功';
        //保存编辑数据后跳转到展示页面
        return redirect('hangye/chubaninfo/'. $Chubaninfos->chuban_id); 
    }

    public function chuban_zengshulist()
    {
        //出版社发布的赠书列表
        //获得用户的uid
        //$name = Session::get('name');
        $uid = Session::get('nameid'); 

        $zengshulists = Chubanzengshu::where('uid',$uid)->orderBy('id', 'desc')->paginate(6);

        foreach ($zengshulists as $zengshulist) {
             //得到书籍介绍
            $zengshulist['books'] = $zengshulist->hasOnebooks;
        }

        //dd($zengshulists);

        return view('hangye.chuban_zengshulist',array(
            'zengshulists' => $zengshulists
        ));
    }

    public function chuban_fabuzengshu()
    {
        //出版社发布赠送的书籍
        return view('hangye.chuban_fabuzengshu');
    }

    public function save_fabuzengshu()
    {
        //保存出版社发布赠送的书籍

        //获得用户的uid
        //$name = Session::get('name');
        $uid = Session::get('nameid'); 

        //得到对应的发布出版社的chuban_id
        $Chubaninfo = Chubaninfo::where('uid',$uid)->first();
        $chuban_id  = $Chubaninfo->chuban_id;

        //发布赠书
        $chubanzengshu = new Chubanzengshu;

        $chubanzengshu -> uid = $uid;
        $chubanzengshu -> chuban_id = $chuban_id;
        $chubanzengshu -> biaoti = trim(request::input('biaoti'));
        $chubanzengshu -> jieshao = request::input('jieshao');

        $zengshulist = trim(request::input('zengshulist'));

        //整理字符串，只保留数字(书籍编号)
        $zengshulist = str_replace('http://www.yedushu.com/info/', '', $zengshulist);
        $zengshulist = str_replace('/', '', $zengshulist);
        $zengshulist = str_replace(' ', '', $zengshulist);

        //去除非数字的字符的数组
        $arr = explode(',', $zengshulist);
        foreach($arr as $k => $v) {
            if(!is_numeric($v)) 
                unset($arr[$k]);
        }

        $zengshulist = implode(',', $arr); // 用逗号分割的字串
        //判断添加的书籍是否存在,不存在则从数组里去掉
        for($i=0; $i<count($arr); $i++){
            if( empty($books = Book::find($arr[$i])) ){
                $zengshulist = str_replace($arr[$i], '', $zengshulist);
            }
        }

        $chubanzengshu -> zengshulist = $zengshulist;
        $chubanzengshu -> add_time = time(); //获取当前时间戳
        $chubanzengshu -> last_time = time();

        //dd($zengshulist);

        //如果赠送的图书不存在，不保存
        if($zengshulist != ''){
            $chubanzengshu -> save();
            return Redirect::to('u/hangye/chuban_zengshulist');
        }
        else{
            //return Redirect::back(); //返回添加页面
            return '您并发布的赠书可能在网站中不存在，或者添加的图书的格式有错误，请核对后重新发布！';
        }
    }

    public function chubanzengshulist($chuban_id)
    {
        //显示对应的出版社赠书列表
        $chubaninfos = Chubaninfo::where('chuban_id',$chuban_id)->get();
        if(empty($chubaninfos[0])){
            //如果出版社不存在
            abort(404);
        }

        $zengshulists = Chubanzengshu::where('chuban_id',$chuban_id)->orderBy('id', 'desc')->paginate(10);

        // if(empty($zengshulists[0])){
        //     //如果发布的赠书不存在
        //     abort(404);
        // }

        foreach ($zengshulists as $zengshulist) {
             //得到书籍介绍
            $zengshulist['books'] = $zengshulist->hasOnebooks;
        }

        //dd($zengshulists);

        return view('hangye.chubanzengshulist',array(
            'chubaninfos'  => $chubaninfos,
            'zengshulists' => $zengshulists
        ));
    }

    public function chubanzengshuinfo($id)
    {
        //显示赠书活动的介绍信息
        $chubanzengshu = Chubanzengshu::find($id);

        if(empty($chubanzengshu)){
            //如果赠送的图书活动不存在
            abort(404);
        }

        $chubaninfos = Chubaninfo::where('chuban_id',$chubanzengshu->chuban_id)->get();

        //得到书籍介绍
        $chubanzengshu['books'] = $chubanzengshu->hasOnebooks;

        $zengshuliuyans = Chubanzengshuliuyan::orderBy('add_time', 'desc')->where('zengshu_id', $id)->paginate(15);

        foreach ($zengshuliuyans as $zengshuliuyan) {
             //得到发表赠书活动评论的用户信息
            $zengshuliuyan['users'] = $zengshuliuyan->hasOnUsersReg;
        }

        //判断是否登录
        if(Session::has('name'))
        {
            //获得用户的uid
            //$name = Session::get('name');
            $uid = Session::get('nameid'); 
            $user = UsersReg::find($uid);
        }
        else
        {
            $user = [];
        }

        //dd($zengshuliuyans);

        //dd($chubaninfos);
        //dd($chubanzengshu);
        //dd($zengshuliuyans);
        return view('hangye.chubanzengshuinfo',array(
            'chubanzengshu'   => $chubanzengshu,
            'chubaninfos'     => $chubaninfos,
            'zengshuliuyans'  => $zengshuliuyans,
            'user'            => $user
        ));
    }

    public function chuban_editzengshu($id)
    {
        //出版社编辑赠书书籍
        //获得用户的uid
        //$name = Session::get('name');
        $uid = Session::get('nameid'); 
        $chubanzengshu = Chubanzengshu::where('uid',$uid)->where('id',$id)->first();
        if(empty($chubanzengshu)){
            //如果赠送的图书活动不存在
            abort(404);
        }

        return view('hangye.chuban_editzengshu',array(
            'chubanzengshu' => $chubanzengshu
        ));
    }

    public function save_editzengshu($id)
    {
        //编辑保存出版社发布赠书活动

        //获得用户的uid
        //$name = Session::get('name');
        $uid = Session::get('nameid'); 

        //需要编辑的赠书活动        
        $chubanzengshu = Chubanzengshu::where('uid',$uid)->where('id',$id)->first();

        $chubanzengshu -> biaoti = trim(request::input('biaoti'));
        $chubanzengshu -> jieshao = request::input('jieshao');
        $zengshulist = trim(request::input('zengshulist'));

        //整理字符串，只保留数字(书籍编号)
        $zengshulist = str_replace('http://www.yedushu.com/info/', '', $zengshulist);
        $zengshulist = str_replace('/', '', $zengshulist);
        $zengshulist = str_replace(' ', '', $zengshulist);

        //去除非数字的字符的数组
        $arr = explode(',', $zengshulist);
        foreach($arr as $k => $v) {
            if(!is_numeric($v)) 
                unset($arr[$k]);
        }

        $zengshulist = implode(',', $arr); // 用逗号分割的字串
        //判断添加的书籍是否存在,不存在则从数组里去掉
        for($i=0; $i<count($arr); $i++){
            if( empty($books = Book::find($arr[$i])) ){
                $zengshulist = str_replace($arr[$i], '', $zengshulist);
            }
        }

        $chubanzengshu -> zengshulist = $zengshulist;
        //$chubanzengshu -> add_time = time(); //获取当前时间戳
        $chubanzengshu -> last_time = time();

        //dd($zengshulist);

        //如果赠送的图书不存在，不保存
        if($zengshulist != ''){
            $chubanzengshu -> save();
            return Redirect::to('u/hangye/chuban_zengshulist');
        }
        else{
            //return Redirect::back(); //返回添加页面
            return '您并发布的赠书可能在网站中不存在，或者添加的图书的格式有错误，请核对后重新发布！';
        }
    }

    public function savezsliuyan($id)
    {
        //保存赠书活动的评论信息
        //获得用户的uid
        //$name = Session::get('name');
        $uid = Session::get('nameid'); 

        //保存赠书活动的留言评论
        $zengshuliuyans = new Chubanzengshuliuyan;

        $zengshuliuyans->uid = $uid;
        $zengshuliuyans->zengshu_id = $id;
        $zengshuliuyans->zsliuyan = trim(request::input('zsliuyan'));
        $zengshuliuyans->add_time = time(); //获取当前时间戳
        $zengshuliuyans->save();

        return Redirect::to('hangye/chubanzengshuinfo/'.$id);
    }

    public function adminzengshulist()
    {
        //出版社赠书活动列表
        $chubanzengshulists = Chubanzengshu::orderBy('id','desc')->paginate(15);
        //dd($chubanzengshulists);
        if(empty($chubanzengshulists)){
            //如果赠送的图书活动不存在
            return '暂时没有发布赠书活动。';
        }
        return view('admin.chubanzengshulist',array(
            'chubanzengshulists'   =>  $chubanzengshulists
        ));
    }

    public function admineditzengshulist($id)
    {
        //编辑出版社赠书活动
        $chubanzengshu = Chubanzengshu::where('id',$id)->first();
        return view('admin.chubaneditzengshulist',array(
            'chubanzengshu'  => $chubanzengshu
        ));
    }

    public function saveadmineditzengshulist($id)
    {
        //保存编辑出版社赠书活动

        //需要编辑的赠书活动        
        $chubanzengshu = Chubanzengshu::where('id',$id)->first();

        $chubanzengshu -> biaoti = trim(request::input('biaoti'));
        $chubanzengshu -> jieshao = request::input('jieshao');
        $zengshulist = trim(request::input('zengshulist'));

        //整理字符串，只保留数字(书籍编号)
        $zengshulist = str_replace('http://www.yedushu.com/info/', '', $zengshulist);
        $zengshulist = str_replace('/', '', $zengshulist);
        $zengshulist = str_replace(' ', '', $zengshulist);

        //去除非数字的字符的数组
        $arr = explode(',', $zengshulist);
        foreach($arr as $k => $v) {
            if(!is_numeric($v)) 
                unset($arr[$k]);
        }

        $zengshulist = implode(',', $arr); // 用逗号分割的字串
        //判断添加的书籍是否存在,不存在则从数组里去掉
        for($i=0; $i<count($arr); $i++){
            if( empty($books = Book::find($arr[$i])) ){
                $zengshulist = str_replace($arr[$i], '', $zengshulist);
            }
        }

        $chubanzengshu -> zengshulist = $zengshulist;
        //$chubanzengshu -> add_time = time(); //获取当前时间戳
        $chubanzengshu -> last_time = time();

        //dd($zengshulist);

        //如果赠送的图书不存在，不保存
        if($zengshulist != ''){
            $chubanzengshu -> save();
            return '赠书活动编辑成功。';
        }
        else{
            //return Redirect::back(); //返回添加页面
            return '您并发布的赠书可能在网站中不存在，或者添加的图书的格式有错误，请核对后重新发布！';
        }
    }

    public function saveadmineditzengshulist_del($id)
    {
        //出版社赠书活动列表 - 删除
        $chubanzengshu = Chubanzengshu::where('id',$id)->first();
        $chubanzengshu->delete();
        return '活动删除成功。';
    }

    public function chubanshelist()
    {
        //版社列表
        $chubaninfos = Chubaninfo::orderBy('chuban_id', 'desc')->paginate(6);
        return view('hangye.chubanshelist',array(
            'chubaninfos' => $chubaninfos
        ));
    }
}
